/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.engine.processors.transformers;

import it.kirys.rilego.engine.loaders.imagesources.SourceRotation;
import java.awt.image.BufferedImage;

/**
 * This class implements the ImageRotator strategy based on the original JEComics
 * code.
 * @author kirys
 */
public class ImageRotator implements IImageRotator {

	public static BufferedImage rotateImage(BufferedImage bi, boolean right) {
		int width = bi.getWidth();
		int height = bi.getHeight();

		BufferedImage biFlip = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
		if (!right) { //rotate left
			for (int i = 0; i < width; i++) {
				for (int j = 0; j < height; j++) {
					biFlip.setRGB(j, width - i - 1, bi.getRGB(i, j));
				}
			}
		} else { //rotate right
			for (int i = 0; i < width; i++) {
				for (int j = 0; j < height; j++) {
					biFlip.setRGB(height - 1 - j, i, bi.getRGB(i, j));
				}
			}
		}
		return biFlip;

	}

	@Override
	public BufferedImage rotate(BufferedImage source, SourceRotation rotation) {
		switch (rotation) {
			case NoRotation:
				return source;
			case RotateHalfTurn:
				return rotateImage(rotateImage(source, true), true);
			case RotateLeft:
				return rotateImage(source, false);
			case RotateRight:
				return rotateImage(source, true);
			default:
				return source;
		}
	}
}
